package com.jsgt.greekfire.ui.mine.user.language

import android.content.Intent
import android.text.TextUtils
import android.view.View
import androidx.core.view.isVisible
import com.fhx.core.base.BaseActivity
import com.fhx.core.utils.SharePreferencesUtil
import com.jsgt.greekfire.App
import com.jsgt.greekfire.R
import com.fhx.core.utils.AppLanguageUtils
import com.jsgt.greekfire.ui.main.MainAtrActivity
import com.jsgt.greekfire.ui.main.MainGroupActivity
import com.jsgt.greekfire.ui.main.launch.LaunchTempActivity
import kotlinx.android.synthetic.main.activity_language.*
import kotlinx.android.synthetic.main.view_title1.*


/**
 * 显示语言
 * author fhx
 * create time 2020/4/23 15:18
 **/
class LanguageActivity : BaseActivity() {

    // 定义保存的文件的名称
//    private val languageName = "sharedfile"
    private val languageName = AppLanguageUtils.LANGUAGE
    private val languageNow by lazy {
        SharePreferencesUtil.getString(this, languageName, "")
    }
    private var languageSel: String? = null
    private var form = 0 //0-从设置中进入 1-从启动页进入

    override fun needFitsSystemWindows(): Boolean {
        return false
    }

    override fun getLayoutId(): Int = R.layout.activity_language

    override fun main() {
        setStatusBar(R.color.transparent, false, R.color.white)

        title = getString(R.string.view_language)
        right_tv.setText(R.string.save)
        form = intent.getIntExtra("from", 0)

//        recyclerview.layoutManager =
//            LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
//        val moneyTypeAdapter = MoneyTypeAdapter()
//        val moneyList = arrayListOf<MoneyBean>()
//        for (i in 0..10) {
//            moneyList.add(MoneyBean())
//        }
//        moneyTypeAdapter.addData(moneyList)
//        recyclerview.adapter = moneyTypeAdapter
        languageSel = languageNow
        when (languageNow) {
            AppLanguageUtils.CHINESE -> {
                langage_cn.isChecked = true
                langage_english.isChecked = false
            }
            AppLanguageUtils.ENGLISH -> {
                langage_cn.isChecked = false
                langage_english.isChecked = true
            }
            else -> {
                langage_cn.isChecked = true
                langage_english.isChecked = false
            }
        }


        /**
         * 中英文，切换关闭
         */
        chinese.setOnClickListener {
            if (!langage_cn.isChecked) {
                langage_cn.isChecked = true
                langage_english.isChecked = false
                languageSel = AppLanguageUtils.CHINESE
                right_tv.visibility = if (TextUtils.equals(languageSel, languageNow)) {
                    View.GONE
                } else {
                    View.VISIBLE
                }
            }
        }

        english.setOnClickListener {
            if (!langage_english.isChecked) {
                langage_cn.isChecked = false
                langage_english.isChecked = true
                languageSel = AppLanguageUtils.ENGLISH
                right_tv.visibility = if (TextUtils.equals(languageSel, languageNow)) {
                    View.GONE
                } else {
                    View.VISIBLE
                }
            }
        }

        right_tv.setOnClickListener {
            switchLanguage(languageSel!!)
        }
    }

    private fun switchLanguage(language: String) {

//        //保存设置语言的类型
        SharePreferencesUtil.saveString(this, languageName, language)

//        AppLanguageUtils.changeAppLanguage(this, language)
        AppLanguageUtils.changeAppLanguage(App.context, language)

        //更新语言后，destroy当前页面，重新绘制
        this.onBackPressed()
        val it = Intent(
            this, if (form == 1) {
                LaunchTempActivity::class.java
            } else {
                MainAtrActivity::class.java
            }
        )
        //清空任务栈确保当前打开activit为前台任务栈栈顶
        it.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        startActivity(it)
    }
}